package com.glela.api.pay.wechat.component.impl;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.glela.api.model.PayAppKeyInfo;
import com.glela.common.util.MD5;
import com.glela.common.util.RandomUtil;

/**
 *@author liujie
 *@2016年11月1日
 */
@Component("weChat0")
public class WeChat0Impl extends AbstractWeChatImpl{

	@Override
	protected Map<String, String> getWeChaPayMap(PayAppKeyInfo payAppKeyInfo, String orderSn, String payNo, int totalFee, String spbillCreateIp, String openId) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("appid", payAppKeyInfo.getAppId());
		map.put("mch_id", payAppKeyInfo.getPartnerId()); //商户号
		//随机数 
		String nonce_str = RandomUtil.getRandomNumber(10);

		map.put("nonce_str", nonce_str);

		StringBuilder body = new StringBuilder();
		body.append(payAppKeyInfo.getStoreName() + "：").append(orderSn);
		map.put("body", body.toString());

		map.put("attach", "APP");

		map.put("out_trade_no", payNo);

		totalFee = totalFee <= 0 ? 999999999 : totalFee;
		map.put("total_fee", String.valueOf(totalFee));

		map.put("spbill_create_ip", spbillCreateIp);

		StringBuilder notify_url = new StringBuilder();
		notify_url.append(payAppKeyInfo.getCallbackUrl()).append("order/wechatPublicNotify/").append(orderSn).append("/").append(payNo);
		map.put("notify_url", notify_url.toString());

		map.put("trade_type", "APP");
		return map;
	}
	
	/**
	 * 微信支付会员
	 */
	@Override
	protected Map<String, String> getWeChaPayMapVip(PayAppKeyInfo payAppKeyInfo, String orderSn, int totalFee,
			String spbillCreateIp, String openId) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("appid", payAppKeyInfo.getAppId());
		map.put("mch_id", payAppKeyInfo.getPartnerId()); // 商户号
		// 随机数
		String nonce_str = RandomUtil.getRandomNumber(10);
		map.put("nonce_str", nonce_str);
		StringBuffer body = new StringBuffer();
		body.append(payAppKeyInfo.getStoreName() + "：").append(orderSn);
		map.put("body", body.toString());
		map.put("attach", "APP");
		map.put("out_trade_no", orderSn);
		totalFee = totalFee <= 0 ? 1 : totalFee;
		map.put("total_fee", String.valueOf(totalFee));
		map.put("spbill_create_ip", spbillCreateIp);
		StringBuffer notify_url = new StringBuffer();
		notify_url.append(payAppKeyInfo.getCallbackUrl()).append("order/wechatPublicNotifyVip");
		map.put("notify_url", notify_url.toString());
		map.put("trade_type", "APP");
		return map;
	}
	
	/**
	 * 微信退款Map
	 * @param map
	 * @param key 
	 * @return
	 * @throws IOException 
	 */
	/*@Override
	protected Map<String, String> weChaPayRefundMap(PayAppKeyInfo payAppKeyInfo, String orderSn, int totalFee,
			String spbillCreateIp, String openId, int refundFee, String afterSn, String payId) throws IOException{
		Map<String, String> map = new HashMap<String, String>();
		map.put("appid", payAppKeyInfo.getAppId());  
		map.put("mch_id", payAppKeyInfo.getPartnerId());  //商户号
		//随机数 
		String nonce_str = RandomUtil.getRandomNumber(10) ;
		map.put("nonce_str", nonce_str); 
		map.put("transaction_id", payId); // 微信订单号	
		map.put("out_trade_no", orderSn); // 父订单号
		map.put("out_refund_no", afterSn); //退款单号   也就是售后单号
		map.put("total_fee", String.valueOf(totalFee));//单位为分     订单总金额
		map.put("refund_fee", String.valueOf(refundFee));// 退款金额   单位为分
		StringBuffer notify_url = new StringBuffer();
		notify_url.append(payAppKeyInfo.getCallbackUrl()).append("order/wechatPublicRefundNotify") ;
		map.put("notify_url", notify_url.toString());
		map.put("op_user_id", payAppKeyInfo.getPartnerId()); //操作员帐号, 默认为商户号   1900000109
		map.put("trade_type", "APP");  
		return map;
		// StringBuffer body = new StringBuffer();
		// body.append(payAppKeyInfo.getStoreName()+"：").append(orderSn);
		// map.put("body", body.toString());  
		//map.put("attach", "APP");
		// totalFee = totalFee <=0 ? 1 : totalFee;
		//map.put("spbill_create_ip", spbillCreateIp); 
	}*/
	
	@Override
	protected void createPaySign(Map<String, Object> outmap,PayAppKeyInfo payAppKeyInfo) throws UnsupportedEncodingException
	{
		outmap.put("timestamp", System.currentTimeMillis() / 1000);
		
		Map<String, String> paySignMap = new HashMap<String ,String >() ;
		paySignMap.put("appid", outmap.get("appid").toString());
		paySignMap.put("partnerid", outmap.get("mch_id").toString());
		paySignMap.put("noncestr", outmap.get("nonce_str").toString());
		paySignMap.put("prepayid", outmap.get("prepay_id").toString());
		paySignMap.put("package", "Sign=WXPay");
		paySignMap.put("timestamp", outmap.get("timestamp").toString());
//		paySignMap.put("signType", "MD5");
		
		//获得支付签名
		StringBuffer paysign = new StringBuffer();
		paysign.append(createSign(paySignMap)).append("&key=").append(payAppKeyInfo.getPartnerKey());
		String paysignStr = MD5.md5(paysign.toString()).toUpperCase();
		
		//将支付签名添加到返回消息中
		outmap.put("paySign", paysignStr) ;
	}

}
